//
//  GHSOD_AfterSaleSelectView.m
//  penzer
//
//  Created by 还带大道 on 2018/11/19.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSOD_AfterSaleSelectView.h"

@interface GHSOD_AfterSaleSelectView()
@property (weak, nonatomic) IBOutlet UIView *picV;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *picV_H;
@property (nonatomic,strong) NSMutableArray<UIView *> *pics;

@end

@implementation GHSOD_AfterSaleSelectView

+ (instancetype)shareView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.backgroundColor = [UIColor whiteColor];
    [self addImages];
}


- (void)addImages
{
    _picV.backgroundColor = [UIColor clearColor];
    _pics = [NSMutableArray array];
    for (NSInteger index =0;index<3;index++) {
        UIButton *btn = [UIButton new];
        //        btn.backgroundColor = [UIColor greenColor];
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_goods"] forState:UIControlStateNormal];
        [self.picV addSubview:btn];
        [self.pics addObject:btn];
    }
    
}


- (void)setStatus:(GHSAfterSaleStatus)status
{
    _status = status;
    switch (status) {
        case GHSAfterSaleStatus_HH_Nomal:{
            self.btn2.hidden = NO;
            [self setupBtn:_btn1 title:@"同意换货" font:[UIFont systemFontOfSize:18] textColor:[UIColor whiteColor] backgroundImage:[UIImage imageNamed:@"按钮3"]];
            [self setupBtn:_btn2 title:@"拒绝换货" font:[UIFont systemFontOfSize:18] textColor:RZ_COLOR(96, 59, 187) backgroundImage:[UIImage imageNamed:@"按钮2"]];
            break;
        }
        case GHSAfterSaleStatus_HH_Refuse:{
            [self setupBtn:_btn2 title:@"拒绝换货" font:[UIFont systemFontOfSize:18] textColor:RZ_COLOR_ALPHA(81, 81, 81, 0.5) backgroundImage:[UIImage imageNamed:@"按钮9"]];
            break;
        }
        case GHSAfterSaleStatus_HH_Agree:{
            self.btn2.hidden = YES;
            [self setupBtn:_btn1 title:@"同意换货" font:[UIFont fontWithName:@"PingFang-SC-Medium" size:18] textColor:RZ_COLOR(81, 81, 81) backgroundImage:[UIImage imageNamed:@"按钮10"]];
            break;
        }
        case GHSAfterSaleStatus_HH_Finished:{
            self.btn2.hidden = YES;
            [self setupBtn:_btn1 title:@"同意换货" font:[UIFont fontWithName:@"PingFang-SC-Medium" size:18] textColor:RZ_COLOR(81, 81, 81) backgroundImage:[UIImage imageNamed:@"按钮10"]];
            break;
        }
        case GHSAfterSaleStatus_TH_Nomal:{
            self.btn2.hidden = NO;
            [self setupBtn:_btn1 title:@"同意退货退款" font:[UIFont systemFontOfSize:18] textColor:[UIColor whiteColor] backgroundImage:[UIImage imageNamed:@"按钮3"]];
            [self setupBtn:_btn2 title:@"拒绝退货退款" font:[UIFont systemFontOfSize:18] textColor:RZ_COLOR(96, 59, 187) backgroundImage:[UIImage imageNamed:@"按钮2"]];
            break;
        }
        case GHSAfterSaleStatus_TH_Refuse:{
            [self setupBtn:_btn2 title:@"拒绝退货退款" font:[UIFont systemFontOfSize:18] textColor:RZ_COLOR_ALPHA(81, 81, 81, 0.5) backgroundImage:[UIImage imageNamed:@"按钮9"]];
            break;
        }
        case GHSAfterSaleStatus_TH_Agree:{
            self.btn2.hidden = YES;
            [self setupBtn:_btn1 title:@"同意退货退款" font:[UIFont fontWithName:@"PingFang-SC-Medium" size:18] textColor:RZ_COLOR(81, 81, 81) backgroundImage:[UIImage imageNamed:@"按钮10"]];
            break;
        }
        case GHSAfterSaleStatus_TH_Finished:{
            self.btn2.hidden = YES;
            [self setupBtn:_btn1 title:@"同意退货退款" font:[UIFont fontWithName:@"PingFang-SC-Medium" size:18] textColor:RZ_COLOR(81, 81, 81) backgroundImage:[UIImage imageNamed:@"按钮10"]];
            break;
        }
            
            
        default:
            break;
    }
    [self layoutIfNeeded];
}




- (void)setupBtn:(UIButton *)btn title:(NSString *)title font:(UIFont *)font textColor:(UIColor *)textColor backgroundImage:(UIImage *)image
{
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = font;
    [btn setTitleColor:textColor forState:UIControlStateNormal];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
}




- (void)layoutSubviews
{
    [super layoutSubviews];
    switch (_status) {
        case GHSAfterSaleStatus_HH_Nomal:{
            break;
        }
        case GHSAfterSaleStatus_HH_Refuse:{
            self.btn2.frame = self.btn1.frame;
            self.btn3.y = CGRectGetMaxY(self.btn2.frame);
            break;
        }
        case GHSAfterSaleStatus_HH_Agree:{
            self.btn3.y = CGRectGetMaxY(self.btn1.frame);
            break;
        }
        case GHSAfterSaleStatus_HH_Finished:{
             self.btn3.y = CGRectGetMaxY(self.btn1.frame);
            break;
        }
        case GHSAfterSaleStatus_TH_Nomal:{
            break;
        }
        case GHSAfterSaleStatus_TH_Refuse:{
            self.btn2.frame = self.btn1.frame;
            self.btn3.y = CGRectGetMaxY(self.btn2.frame);
            break;
        }
        case GHSAfterSaleStatus_TH_Agree:{
            self.btn3.y = CGRectGetMaxY(self.btn1.frame);
            break;
        }
        case GHSAfterSaleStatus_TH_Finished:{
            self.btn3.y = CGRectGetMaxY(self.btn1.frame);
            break;
        }
            
        default:
            break;
    }
    
    
    //计算图片高度
    CGFloat wh = 80;
    __block CGFloat x = 0;
    __block CGFloat y = 0;
    __block NSInteger row = 0;
    __block NSInteger col = 0;
    NSInteger maxcount = 3;
    CGFloat margin_xy = 10;
    
    [self.pics enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        col = idx % maxcount;
        row = idx / maxcount;
        x = (wh + margin_xy)*col;
        y = (wh + margin_xy)*row;
        obj.frame = CGRectMake(x, y, wh, wh);
    }];
    
    _picV_H.constant = CGRectGetMaxY(self.pics.lastObject.frame);
    
    _contentH = CGRectGetMaxY(self.btn3.frame) + 20 + CGRectGetMaxY(self.pics.lastObject.frame) - self.picV.height;
}




- (CGFloat)contentH
{
    [self layoutIfNeeded];
    return _contentH;
}


- (IBAction)onClickBtn1 {
    !self.itemOnClick?:self.itemOnClick(0);
}

- (IBAction)onClickBtn2 {
    !self.itemOnClick?:self.itemOnClick(1);
}

- (IBAction)onClickBtn3 {
    !self.itemOnClick?:self.itemOnClick(2);
}


@end
